﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;

namespace PizzaWic.admin
{
    /// <summary>
    /// permet d'ajouter un nouveau produit en base
    /// </summary>
    public partial class AddProduct : System.Web.UI.Page
    {
        public static List<int> rawMaterialListAdd = new List<int>();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //premier acces pour obtenir tous les raw material
                SqlAccess sql = new SqlAccess();
                DataSet dataSet = new DataSet();
                dataSet = sql.getAllRawMaterial();

                rmList.DataSource = dataSet;
                rmList.DataBind();
                rawMaterialListAdd = new List<int>();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.addProduct();
            Response.Redirect("ProductManagement.aspx");
        }

        protected void addMoreP_Click(object sender, EventArgs e)
        {
            this.addProduct();
            this.name.Text = "";
            this.prix.Text = "";
        }

        /// <summary>
        /// ajoute le produit
        /// </summary>
        private void addProduct()
        {
            //télécharge l'image sur le serveur
            if (fileUploader.HasFile)
            {
                if (File.Exists(Server.MapPath("../images/imageUser/") + Path.GetFileName(fileUploader.FileName)))
                {
                    File.Delete(Server.MapPath("../images/imageUser/") + Path.GetFileName(fileUploader.FileName));
                }
                fileUploader.SaveAs(Server.MapPath("../images/imageUser/") + Path.GetFileName(fileUploader.FileName));
            }

            //liste des données d'un produit
            SqlAccess sqlAccess = new SqlAccess();
            Object[] data = new Object[4];
            data[0] = this.name.Text;
            data[1] = type.SelectedValue.ToString();
            data[2] = this.prix.Text;
            data[3] = "imageUser/" + Path.GetFileName(fileUploader.FileName);
            sqlAccess.addProduct(data);

            //liens entre les raw_material et les produits
            int productId = sqlAccess.getLastIdProduct();
            foreach (int i in rawMaterialListAdd)
            {
                data = new Object[2];
                data[0] = productId;
                data[1] = i;
                sqlAccess.addProductRawMaterial(data);
            }
        }


        /// <summary>
        /// permet d'actualiser la liste des ingredients (raw_material) du produit
        /// </summary>
        protected void addRawMaterialToProduct(Object sender, EventArgs args)
        {
            CheckBox box = sender as CheckBox;

            RepeaterItem item = box.NamingContainer as RepeaterItem;
            HiddenField hf = item.FindControl("HiddenFieldrm") as HiddenField;
            int id = int.Parse(hf.Value);
            if (box.Checked)
                rawMaterialListAdd.Add(id);
            else
                rawMaterialListAdd.Remove(id);
        }
    }
}